package com.zkthink.ceres.member.controller;

import com.zkthink.base.R;
import com.zkthink.ceres.member.entity.SignIn;
import com.zkthink.ceres.member.entity.SignInHis;
import com.zkthink.ceres.member.service.SignInHisService;
import com.zkthink.ceres.member.service.SignInService;
import com.zkthink.ceres.member.vo.SignInHisVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * FileName: IntegralController
 * Author:   Pepis
 * Date:   2020  2020/8/29 15:45
 * Description: 会员签到 控制器
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/sign")
@Api(value = "SIGN", tags = "签到")
public class SignInController {
    @Resource
    private SignInService signInService;
    @Resource
    private SignInHisService signInHisService;


    /**
     * 会员签到
     */
    @ApiOperation(value = "会员签到")
    @GetMapping(value = "/signIn/{mid}")
    @Transactional
    public R signIn(@PathVariable("mid") Long mid) {
        SignIn singnByMid = signInService.getSingnByMid(mid);
        //第一次签到

        //第二次
        if (singnByMid == null) {
            SignIn s = new SignIn();
            s.setMid(mid);
            s.setContinueDays(1);
            signInService.save(s);
            SignInHis signInHis = new SignInHis();
            signInHis.setMid(mid);
            signInHisService.save(signInHis);
        }
        if (singnByMid != null) {
            LocalDate signInTime = singnByMid.getUpdateTime().toLocalDate();
            LocalDate currTime = LocalDate.now();
            long daysDiff = ChronoUnit.DAYS.between(signInTime, currTime);
            if (daysDiff <= 0) {
                return R.fail("重复签到");
            }
            if (daysDiff > 1) {
                // 1, 超过一天, 把连续签到的天数重置为 1
                singnByMid.setContinueDays(1);
            } else {
                // 2, 没有超过一天, 把连续签到的天数+1
                singnByMid.setContinueDays(singnByMid.getContinueDays() + 1);
            }
            singnByMid.setUpdateTime(null);
            signInService.updateById(singnByMid);
            SignInHis signInHis = new SignInHis();
            signInHis.setMid(mid);
            signInHisService.save(signInHis);
        }

        //计算贡献值
//        第1天/第2天/第3天/第4天/第5天/第6天/第7天：
//        可获得：8贡献值/16贡献值/24贡献值/32贡献值/40贡献值/48贡献值/888贡献值。
//        持续签到七天为一个循环，如未持续签到以第一天“8”贡献值计算。
        /**
         * 20210401
         * 每日签到： 第1天/第2天/第3天/第4天/第5天/第6天/第7天：
         * 分别可获得：8贡献值/16贡献值/24贡献值/32贡献值/40贡献值/48贡献值/888贡献值。(188)
         * 持续签到七天为一个循环，如未持续签到，再签到，从第一天“8”贡献值计算。
         */
        SignIn singnByMid1 = signInService.getSingnByMid(mid);
        LocalDate currTime = LocalDate.now();
        Integer days = singnByMid1.getContinueDays();
        return R.success();
    }

    /**
     * 查询当月签到信息
     */
    @ApiOperation(value = "查询当月签到信息")
    @GetMapping(value = "/search/{mid}")
    public R<SignInHisVO> searchSignIn(@PathVariable("mid") Long mid) {
        //这个月的第一天
        LocalDateTime firstDay = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
        //这个月的第二天
        LocalDateTime lastDay = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX);
        return signInHisService.seachByFristAndEndDay(firstDay, lastDay, mid);
    }


}
